package com.shxc.smarttrade.old.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.shxc.smarttrade.R;
import com.shxc.smarttrade.MyApplication;
import com.shxc.smarttrade.old.constants.URLContents;
import com.shxc.smarttrade.old.myview.CommonDialog;
import com.shxc.smarttrade.old.myview.CustomTextView;
import com.shxc.smarttrade.old.utils.MyOkHttpUtils;
import com.shxc.smarttrade.old.utils.SpUtils;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

import static com.mob.MobSDK.getContext;

/**
 * Created by jcdev1 on 2017/10/17.
 */

public class UseMTFAccountActivity extends BaseActivity {
    @BindView(R.id.rl_left)
    AutoRelativeLayout rlLeft;
    @BindView(R.id.tv_center)
    TextView tvCenter;
    @BindView(R.id.tv_mtf_id)
    CustomTextView tvMtfId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mtfour_login);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        tvCenter.setText("MT4登录");
        tvMtfId.setText(getIntent().getStringExtra("mtfAccount"));
    }

    @OnClick({R.id.rl_left, R.id.btn_mtf_pass, R.id.tv_mtf_course})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_left:
                finish();
                break;
            case R.id.btn_mtf_pass:
                showAlertDialog();
                break;
            case R.id.tv_mtf_course:
                getMTFCurse();
                break;
        }
    }

    private void showAlertDialog() {

        CommonDialog commonDialog = new CommonDialog(this, R.style.customDialogStyle);
        commonDialog.setCanceledOnTouchOutside(true);
        commonDialog.setBaseText("提示", "是否获取MT4密码？", "取消", "确定");
        commonDialog.setYesCLickRunAble(new Runnable() {
            @Override
            public void run() {
                showShortDialog();
                getMT4Password();
            }
        });
        commonDialog.show();

    }


    /**
     * 获取MT4账号密码
     */
    public void getMT4Password() {
        Map<String, Object> map = new HashMap<>();
        map.put("accessToken", SpUtils.getString(this, "accessToken", ""));
        map.put("userId", SpUtils.getInt(this, "userId", -1) + "");
        MyOkHttpUtils.getServiceDatas(this,map, URLContents.ACCOUNT_FRAGMENT_MTPASS, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void faild(Call call, Exception e, int id) {
                MyApplication.showToast(getContext(), "网络异常");
                dismissShortDialog();
            }

            @Override
            public void success(String response, int id) {
                JsonObject ob = new JsonParser().parse(response).getAsJsonObject();
                String errCode = "";
                String errMsg = "";
                if (!ob.get("err_code").isJsonNull())
                    errCode = ob.get("err_code").getAsString();
                if (!ob.get("err_msg").isJsonNull())
                    errMsg = ob.get("err_msg").getAsString();
                if (errCode.equals("0"))
                    MyApplication.showToast(getContext(), "获取成功！");
                else
                    MyApplication.showToast(getContext(), errMsg);

                dismissShortDialog();

            }
        });
    }


    public void getMTFCurse() {
        Intent intent = new Intent(this, HFiveActivity.class);
        intent.putExtra("name", "MT4教程");
        intent.putExtra("url", URLContents.MTF_USE_DETAIL);
        startActivity(intent);
    }
}
